﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CSharpDesign.BLL;
using CSharpDesign.DAL;

namespace CSharpDesign
{
    public partial class ShangPinList : System.Web.UI.Page
    {
        SqlHelper sqloperate = new SqlHelper();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = " 水果商店";
            if (!IsPostBack)
            {
                gvbind();
            }
        }
        protected string CutChar(string strChar, int intLength)
        {
            //取得自定义长度的字符串
            if (strChar.Length > intLength)
            { return strChar.Substring(0, intLength); }
            else
            { return strChar; }

        }
        private void gvbind()
        {
           try {
                ShangPinListService sps = new ShangPinListService();
                string sqlstr = "";
                sqlstr = "select  * from ShangPin order by SPID desc ";
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

                con.Open();

                SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);

                DataSet ds = new DataSet();
                sda.Fill(ds);
                PagedDataSource objPds = new PagedDataSource();
                objPds.DataSource = ds.Tables[0].DefaultView;

                objPds.AllowPaging = true;
                objPds.PageSize = 16;
                string page = Request.QueryString["Page"];
                int CurPage;               
                //if (page != null)
                //    CurPage = Convert.ToInt32(page);
                //else
                //    CurPage = 1;
                CurPage = sps.Getpage(page);
                objPds.CurrentPageIndex = CurPage - 1;
                lblCurrentPage.Text = CurPage.ToString();
                lblSumPage.Text = objPds.PageCount.ToString();

                if (!objPds.IsFirstPage)
                {
                    this.hyfirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + 1;
                    lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
                }

                if (!objPds.IsLastPage)
                {
                    hylastpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + objPds.PageCount;

                    lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
                }

                this.DataList1.DataSource = objPds;
                this.DataList1.DataBind();
                con.Close();


            }
            catch (Exception ex) { 
                throw ex;
            }
            
        }
    }
}